package com.oa.srms.apps.actions.organization;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import net.sf.json.JSONObject;

import com.oa.srms.apps.actions.BasicAction;
import com.oa.srms.apps.business.organization.IPostCategoryService;
import com.oa.srms.apps.business.organization.IPostInfoService;
import com.oa.srms.apps.business.organization.ISectionInfoService;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.entity.organization.PostCategoryValue;
import com.oa.srms.apps.entity.organization.PostInfoValue;
import com.oa.srms.apps.entity.organization.SectionInfoValue;

@SuppressWarnings("serial")
public class PostInfoAction extends BasicAction {
	
	private List<PostInfoValue> infoList;
	private List<PostCategoryValue> categoryList;
	private List<SectionInfoValue> sectionList;
	@Autowired
	private IPostCategoryService categoryService;
	@Autowired
	private ISectionInfoService sectionService;
	@Autowired
	private IPostInfoService infoService;

	private String postIds;
	private Integer postId;
	private String postInfoJsonString;
	private Integer sectionId;
	private Integer sectionNo;
	private Integer categoryId;
	private Integer state;
	
	private String str;

	public String execute() throws Exception {
		totalpage = infoService.countTPages(categoryId, sectionNo, state, rows);
		count = infoService.getTotalPages();
			if(postId == null && postIds == null){
				currpage=totalpage;
			}
			else{
				if(count%rows==0&&totalpage+1==currpage){
					currpage=totalpage;
				}
			}
		infoList = infoService.queryPostInfoWithPage(categoryId, sectionNo, state, rows, currpage);
		categoryList = categoryService.queryPostCategoryByStates(IDEDStates.ENABLE);
		sectionList = sectionService.querySectionInfo(null, IDEDStates.ENABLE);
		return "show";
	}
	
	public String delete() throws Exception{
		if(infoService.deletePostInfo(postIds)){
			return "delete";
		}
		return "false";
	}
	
	public String toDisable() throws Exception{
		if(infoService.toDisable(postId)){
			return "toDisable";
		}
		return "false";
	}
	
	public String toEnable() throws Exception{
		if(infoService.toEnable(postId)){
			return "toEnable";
		}
		return "false";
	}
	
	public String add() throws Exception{
		Integer postId = null;
		String postNo = null;
		String postName = null;
		Integer categoryId = null;
		Integer sectionId = null;
		try {
			JSONObject json =   JSONObject.fromObject(postInfoJsonString);
			if(!json.get("postId").equals("")){
				postId = json.getInt("postId");
			}
			if(!json.get("postNo").equals("")){
				postNo= json.getString("postNo");
			}
			if(!json.get("postName").equals("")){
				postName = json.getString("postName");;
			}
			if(!json.get("postCategoryId").equals("")){
				categoryId = json.getInt("postCategoryId");
			}
			if(!json.get("sectionId").equals("")){
				sectionId= json.getInt("sectionId");
			}else{
				sectionId = 0;
			}
			if(postId == null){
				if(infoService.addPostInfo(categoryId, sectionId, postName, postNo) > 0){
					str = "1";
				}else{
					str= "-1";
				}
			}else{ 
				if(infoService.modifyPostInfo(postId, categoryId, sectionId, postName, postNo)){
					str = "1";
				}else{
					str = "-1";
				}
			}
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().write(str);
		} catch (Exception e) {
			return "false";
		}
		return null;
	}
	
	public String addCategory() throws Exception{
		String categoryName = null;
		try {
			JSONObject json =   JSONObject.fromObject(postInfoJsonString);
			if(!json.get("categoryName").equals("")){
				categoryName = json.getString("categoryName");
			}
			if(postId == null){
				categoryService.addPostCategory(categoryName);
			}
		} catch (Exception e) {
			return "false";
		}
		return null;
	}

	public List<PostInfoValue> getInfoList() {
		return infoList;
	}

	public void setInfoList(List<PostInfoValue> infoList) {
		this.infoList = infoList;
	}

	public List<PostCategoryValue> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<PostCategoryValue> categoryList) {
		this.categoryList = categoryList;
	}

	public List<SectionInfoValue> getSectionList() {
		return sectionList;
	}

	public void setSectionList(List<SectionInfoValue> sectionList) {
		this.sectionList = sectionList;
	}

	public String getPostIds() {
		return postIds;
	}

	public void setPostIds(String postIds) {
		this.postIds = postIds;
	}

	public Integer getPostId() {
		return postId;
	}

	public void setPostId(Integer postId) {
		this.postId = postId;
	}

	public String getPostInfoJsonString() {
		return postInfoJsonString;
	}

	public void setPostInfoJsonString(String postInfoJsonString) {
		this.postInfoJsonString = postInfoJsonString;
	}

	public Integer getSectionId() {
		return sectionId;
	}

	public void setSectionId(Integer sectionId) {
		this.sectionId = sectionId;
	}

	public Integer getSectionNo() {
		return sectionNo;
	}

	public void setSectionNo(Integer sectionNo) {
		this.sectionNo = sectionNo;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}
	
}
